<?php

namespace app\admin\model\adv;

use think\helper\Time;
use think\Model;
use think\model\relation\HasMany;
use traits\model\SoftDelete;

class Lists extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'adv_lists';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 统计pv，uv
     * @param $type
     * @param $start
     * @param $end
     * @return int
     */
    public function getCount($ids,$type, $start, $end){
        $list = Count::where([
            'adv_lists_id' => $ids,
            'createtime' => ['between',[$start,$end]]
        ])->select();
        if($type == 'pv'){
            return count($list);
        }elseif($type == 'uv'){
            $newList = [];
            foreach ($list as $val){
                if(!in_array($val['ip'],$newList)){
                    $newList[] = $val['ip'];
                }
            }
            return count($newList);
        }
    }

    public function html()
    {
        return $this->belongsTo('Html', 'adv_html_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
